package com.yaoxun.bored.controller.sys;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.websocket.server.PathParam;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.user.User;
import com.yaoxun.bored.model.sys.user.UserQuery;
import com.yaoxun.bored.service.sys.user.UserService;
import com.yaoxun.bored.shiro.credential.PasswordHashedHelper;
import com.yaoxun.bored.util.enums.JsonStatus;
import com.yaoxun.bored.validator.ValidatorHelper;

import tk.mybatis.mapper.entity.Example;

@RequestMapping("/user")
@Controller
public class UserController extends BaseController {
	
	@Inject
	private PasswordHashedHelper passwordHashedHelper;
	
	@Inject
	private ValidatorHelper validatorHelper;
	
	@Inject
	private UserService userService;
	
	
	/**
	 * 用户管理页面
	 * @author Loren
	 * @createTime 2018年1月24日 下午4:11:33
	 * @return
	 */
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public String index() {
		return "sys/user/user";
	}
	
	/**
	 * 新增页面
	 * @author Loren
	 * @createTime 2018年1月27日 上午10:52:28
	 * @return
	 */
	@RequestMapping(value="save", method=RequestMethod.GET)
	public String edit() {
		return "sys/user/user_edit";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(User user) {
		JsonObject jsonObject = new JsonObject(JsonStatus.SUCCESS);
		BindingResult bindingResult = validatorHelper.validate(user);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return jsonObject;
		}
		String salt = passwordHashedHelper.createSalt();
		String pwd = passwordHashedHelper.hashed(user.getPwd(), salt);
		user.setPwd(pwd);
		user.setSalt(salt);
		user.setCreateTime(new Date());
		user.setCreateId(getLoginUserId());
		userService.insert(user);		
		return jsonObject;
	}
	
	/**
	 * 判断账号是否已经存在
	 * @author Loren
	 * @createTime 2018年2月2日 下午4:12:16
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/exist")
	@ResponseBody
	public String existAccount(@RequestParam String account) {
		Example example = new Example(User.class);
		example.createCriteria().andEqualTo("account", account);
		int count = userService.selectCountByExample(example);
		if(count > 0) {
			return JsonStatus.FAIL.value();
		}else {			
			return JsonStatus.SUCCESS.value();
		}
	}
	
	@RequestMapping(value="/search")
	@ResponseBody
	public PageResult search(UserQuery userQuery) {
		PageInfo<Map<String,Object>> info = userService.selectByPaging(userQuery);
		PageResult result = new PageResult();
		result.setRows(info.getList());
		result.setTotal(info.getTotal());
		return result;
	}
		
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView("sys/user/user_edit");
		User user = userService.selectByPrimaryKey(id);
		mv.addObject("obj", user);
		return mv;		
	}
	
	
}
